package com.opencloud.ywt.provider.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.ResultBody;
import com.opencloud.iot.client.model.entity.IotDayMoveResult;
import com.opencloud.iot.client.model.entity.IotDaySleepResult;
import com.opencloud.ywt.client.model.entity.*;
import com.opencloud.ywt.provider.service.BhyfBdbService;
import com.opencloud.ywt.provider.service.BhyfBodyService;
import com.opencloud.ywt.provider.service.BhyfStudentService;
import com.opencloud.ywt.provider.service.feign.IotRemoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.synth.Region;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 园务通（学生）
 * @author yeyong
 */
@Slf4j
@RestController
@Api(value = "园务通", tags = "园务通")
public class BhyfStudentController {
    @Autowired
    private BhyfStudentService studentService;

    @Autowired
    private IotRemoteService iotRemoteService;

    @Autowired
    private BhyfBdbService bhyfBdbService;

    /**
     * 获取园所学生列表
     * @param gid
     * @param classid
     * @return
     */
    @ApiOperation(value = "获取园所新手环学生列表", notes = "获取园所新手环学生列表")
    @RequestMapping(value="/ywt/studentlist",method= RequestMethod.GET)
    public ResultBody<List<BhyfStudent>> getBhyfStudentList(
            @RequestParam(value = "gid",required = true) final Integer gid,
            @RequestParam(value = "classid",required = true) final Integer classid
    )
    {
        List<BhyfStudent> students =  studentService.getBhyfStudentList(gid,0,classid,"",0);
        return ResultBody.ok().data(students);
    }
    /**
     * 获取学生每日睡眠列表
     * @param thisdate
     * @param gid
     * @param classid
     * @param slpstatus
     * @return
     */
    @ApiOperation(value = "获取学生每日睡眠列表", notes = "获取学生每日睡眠列表")
    @GetMapping("/bhyfstudent/sleepdaylist")
    public ResultBody<List<BhyfDaySleepResult>> getStudentDaySleepList(@RequestParam(required = true) String thisdate, @RequestParam(required = true) Integer gid, @RequestParam(required = true) Integer classid, @RequestParam(required = false) Integer slpstatus
    )
    {
        List<BhyfDaySleepResult> results = new ArrayList<>();
        List<BhyfStudent> students = studentService.getBhyfStudentList(gid,0,classid,"",0);
        for (int i=0;i<students.size();i++)
        {
            BhyfDaySleepResult model = new BhyfDaySleepResult();
            BhyfStudent entry = students.get(i);
            model.setStudent(entry);
            List<IotDaySleepResult> daySleepResults = iotRemoteService.getIotDaySleepResult(entry.getMac(),slpstatus,thisdate);
            if(daySleepResults.size()>0) {
                model.setIotDaySleepResult(daySleepResults.get(0));
            }
            results.add(model);
        }
        return ResultBody.ok().data(results);
    }

    /**
     * 获取学生每周睡眠统计
     * @param startDate
     * @param endDate
     * @param gid
     * @param classid
     * @return
     */
    @ApiOperation(value = "获取学生每周睡眠统计", notes = "获取学生每周睡眠统计")
    @GetMapping("/bhyfstudent/sleepweeklist")
    public ResultBody<List<BhyfWeekSleepResult>> getStudentSleepWeekList(
            @RequestParam(required = true) String startDate,
            @RequestParam(required = true) String endDate,
            @RequestParam(required = true) Integer gid,
            @RequestParam(required = true) Integer classid
    )
    {
        List<BhyfWeekSleepResult> results = new ArrayList<>();
        //取班级学生数据
        List<BhyfStudent> students = studentService.getBhyfStudentList(gid,0,classid,"",0);
        List<String> watchList = new ArrayList<>();
        for(int i=0;i<students.size();i++){
            watchList.add(students.get(i).getMac());
        }
        //取时间段内的日睡眠结果数据
        //List<IotDaySleepResult> sleepResults = iotRemoteService.getIotDaySleepResult("",-1,"",startDate,endDate);
        //过滤符合学生数据的睡眠结果
        //sleepResults = sleepResults.stream().filter((IotDaySleepResult s) -> students.contains(s.getWatchmac())).collect(Collectors.toList());
        //遍历日期
        Date ss = DateUtil.parseDate(startDate);
        Date ee = DateUtil.parseDate(endDate);
        int days = ee.getDay()-ss.getDay();
        for(int i = 0;i<days;i++)
        {
            String thisdate = DateUtil.formatDate(DateUtil.offsetDay(ss,i)).substring(0,10);
            //取时间段内的日睡眠结果数据
            List<IotDaySleepResult> sleepResults = iotRemoteService.getIotDaySleepResult("",-1,thisdate);
            //过滤符合学生数据的睡眠结果
            sleepResults = sleepResults.stream()
                    .filter((IotDaySleepResult s) -> watchList.contains(s.getWatchmac().toLowerCase())).collect(Collectors.toList());
            BhyfWeekSleepResult model = new BhyfWeekSleepResult();
            model.setClassid(classid);
            model.setThisdate(thisdate);
            model.setDayname(DateUtil.dayOfWeekEnum(DateUtil.offsetDay(ss,i)).toChinese());
            if(sleepResults!=null && sleepResults.size()>0) {
                model.setAvgvalue(sleepResults.stream().mapToDouble(IotDaySleepResult::getSleeptime).average().getAsDouble());
                model.setGoodcount(sleepResults.stream().filter((IotDaySleepResult s) -> s.getSlpstatus() == 3).collect(Collectors.toList()).size());
                model.setLowcount(sleepResults.stream().filter((IotDaySleepResult s) -> s.getSlpstatus() == 2).collect(Collectors.toList()).size());
                model.setNocount(sleepResults.stream().filter((IotDaySleepResult s) -> s.getSlpstatus() == 1).collect(Collectors.toList()).size());
                model.setNodata(students.size() - sleepResults.stream().filter(distinctByKey(s -> s.getWatchmac())).collect(Collectors.toList()).size());
            }
            results.add(model);
        }
        return ResultBody.ok().data(results);
    }

    /**
     * 获取学生每日运动列表
     * @param thisdate
     * @param gid
     * @param classid
     * @return
     */
    @ApiOperation(value = "获取学生每日运动列表", notes = "获取学生每日运动列表")
    @GetMapping("/bhyfstudent/movedaylist")
    public ResultBody<List<BhyfDayMoveResult>> getDayMoveList(@RequestParam(required = true) String thisdate, @RequestParam(required = true) Integer gid, @RequestParam(required = true) Integer classid
    )
    {
        List<BhyfDayMoveResult> results = new ArrayList<>();
        List<BhyfStudent> students = studentService.getBhyfStudentList(gid,0,classid,"",0);
        for (int i=0;i<students.size();i++)
        {
            BhyfDayMoveResult model = new BhyfDayMoveResult();
            BhyfStudent entry = students.get(i);
            model.setStudent(entry);
            List<IotDayMoveResult> dayMoveResult = iotRemoteService.getIotDayMoveResult(entry.getMac(),thisdate);
            if(dayMoveResult.size()>0) {
                model.setIotDayMoveResult(dayMoveResult.get(0));
            }
            results.add(model);
        }
        return ResultBody.ok().data(results);
    }

    /**
     * 获取园所区角列表
     * @param gid
     * @param classid
     * @return
     */
    @ApiOperation(value = "获取园所区角列表", notes = "获取园所区角列表")
    @RequestMapping(value="/ywt/anglelist",method= RequestMethod.GET)
    public ResultBody<List<BhyfBflAngle>> getBhyfAngleList(
            @RequestParam(value = "gid",required = false) Integer gid,
            @RequestParam(value = "classid",required = false) Integer classid,
            @RequestParam(value = "isspecial",required = false) Integer isspecial
    )
    {
        if(isspecial==null)isspecial=0;
        List<BhyfBflAngle> angles =  studentService.getBhyfClassAngleList(gid,classid);
        if(isspecial==0) {
            angles = angles.stream().filter(s -> s.getAngletype() < 8).collect(Collectors.toList());
        }
        for(int i=0;i<angles.size();i++)
        {
            BhyfBflAngle angle = angles.get(i);
            List<BhyfBflAngleBeacon> beacons = studentService.getBhyfClassAngleBeaconList(angle.getId());
            angle.setBeaconList(beacons);
        }
        return ResultBody.ok().data(angles);
    }

    /**
     * 获取园所区角的信标列表
     * @param angleid
     * @return
     */
    @ApiOperation(value = "获取园所区角的信标列表", notes = "获取园所区角的信标列表")
    @RequestMapping(value="/ywt/anglebeaconlist",method= RequestMethod.GET)
    public ResultBody<List<BhyfBflAngleBeacon>> getBhyfAngleBeaconList(
            @RequestParam(value = "angleid",required = false) final Integer angleid
    )
    {
        List<BhyfBflAngleBeacon> angleBeacons =  studentService.getBhyfClassAngleBeaconList(angleid);
        return ResultBody.ok().data(angleBeacons);
    }

    /**
     * 根据信标获取区角信息
     * @param beacon
     * @return
     */
    @ApiOperation(value = "根据信标获取区角信息", notes = "根据信标获取区角信息")
    @RequestMapping(value="/ywt/anglebybeacon",method= RequestMethod.GET)
    public ResultBody<List<BhyfBflAngle>> getAngleByBeacon(
            @RequestParam(value = "beacon",required = false) final String beacon
    )
    {
        List<BhyfBflAngle> angles =  studentService.getAngleByBeacon(beacon);
        for(int i=0;i<angles.size();i++)
        {
            BhyfBflAngle angle = angles.get(i);
            List<BhyfBflAngleBeacon> beacons = studentService.getBhyfClassAngleBeaconList(angle.getId());
            angle.setBeaconList(beacons);
        }
        return ResultBody.ok().data(angles);
    }

    /**
     * 新增区角信息
     * @param anglename
     * @param angletype
     * @param classid
     * @param classname
     * @param gid
     * @param gradenum
     * @return
     */
    @ApiOperation(value = "新增区角信息", notes = "新增区角信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "anglename", value = "区角名称", required = true, paramType = "form"),
            @ApiImplicitParam(name = "angletype", value = "区角类型", required = true, paramType = "form"),
            @ApiImplicitParam(name = "classid", value = "班级ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "classname", value = "班级名称", required = true, paramType = "form"),
            @ApiImplicitParam(name = "gid", value = "园ID", required = true, paramType = "form"),
            @ApiImplicitParam(name = "gradenum", value = "年级", required = true, paramType = "form"),
            @ApiImplicitParam(name = "makeobject", value = "建设目标", required = false, paramType = "form"),
            @ApiImplicitParam(name = "makemethod", value = "建设策略", required = false, paramType = "form")
    })
    @PostMapping("/ywt/addbhyfbflangle")
    public ResultBody<BhyfBflAngle> addBhyfBflAngle(
            @RequestParam(name = "anglename") String anglename,
            @RequestParam(name = "angletype") Integer angletype,
            @RequestParam(name = "classid") Integer classid,
            @RequestParam(name = "classname") String classname,
            @RequestParam(name = "gid") Integer gid,
            @RequestParam(name = "gradenum") Integer gradenum,
            @RequestParam(name = "makeobject") String makeobject,
            @RequestParam(name = "makemethod") String makemethod
    )
    {
        BhyfBflAngle entiy = new BhyfBflAngle();
        entiy.setAnglename(anglename);
        entiy.setAngletype(angletype);
        entiy.setClassid(classid);
        entiy.setClassname(classname);
        entiy.setGid(gid);
        entiy.setGradenum(gradenum);
        entiy.setStatus(1);
        if(makemethod!=null) {
            entiy.setMakemethod(makemethod);
        }
        if(makeobject!=null) {
            entiy.setMakeobject(makeobject);
        }
        return ResultBody.ok().data(studentService.addBhyfBflAngle(entiy));
    }


    /**
     * 编辑区角信息
     * @param id
     * @param anglename
     * @param angletype
     * @return
     */
    @ApiOperation(value = "编辑区角信息", notes = "编辑区角信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区角id", required = true, paramType = "form"),
            @ApiImplicitParam(name = "anglename", value = "区角名称", required = true, paramType = "form"),
            @ApiImplicitParam(name = "angletype", value = "区角类型", required = true, paramType = "form"),
            @ApiImplicitParam(name = "makeobject", value = "建设目标", required = false, paramType = "form"),
            @ApiImplicitParam(name = "makemethod", value = "建设策略", required = false, paramType = "form")
    })
    @PostMapping("/ywt/updatebhyfbflangle")
    public ResultBody<Integer> updateBhyfBflAngle(
            @RequestParam(name = "id") Integer id,
            @RequestParam(name = "anglename") String anglename,
            @RequestParam(name = "angletype") Integer angletype,
            @RequestParam(name = "makeobject") String makeobject,
            @RequestParam(name = "makemethod") String makemethod
    )
    {
        BhyfBflAngle entiy = studentService.getBhyfBflAngleById(id);
        entiy.setAngletype(angletype);
        entiy.setAnglename(anglename);
        if(makeobject!=null){entiy.setMakeobject(makeobject);}
        if(makemethod!=null){entiy.setMakemethod(makemethod);}
        return ResultBody.ok().data(studentService.updateBhyfBflAngle(entiy));
    }

    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object,Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    /**
     * 获取班级睡眠数据的结果
     * @param map
     * @param startTime
     * @param endTime
     * @return
     */
    @ApiOperation(value = "获取班级睡眠数据的结果", notes = "获取班级睡眠数据的结果")
    @RequestMapping(value="/ywt/xhbsleepclasslist",method= RequestMethod.GET)
    public ResultBody<List<BhyfXhbSleepClass>> getXhbSleepClassList(
            @RequestParam(required = false) Map map,
            @RequestParam(value = "startTime",required = false) final String startTime,
            @RequestParam(value = "endTime",required = false) final String endTime
    )
    {
        //IPage<BhyfXhbSleepClass> bhyfXhbSleepClasses =  studentService.findBhyfXhbSleepClassListPage(new PageParams(map),startTime,endTime);
        PageParams pageParams = new PageParams(map);
        BhyfXhbSleepClass query = pageParams.mapToObject(BhyfXhbSleepClass.class);
        List<BhyfXhbSleepClass> bhyfXhbSleepClasses =  bhyfBdbService.findClassSleepList(query.getGardenid(),query.getGradenum(),query.getClassid(),startTime,endTime);
        return ResultBody.ok().data(bhyfXhbSleepClasses);
    }

    /**
     * 获取学生睡眠数据的结果
     * @param gid
     * @param classid
     * @param startTime
     * @param endTime
     * @param studentname
     * @return
     */
    @ApiOperation(value = "获取学生睡眠数据的结果", notes = "获取学生睡眠数据的结果")
    @RequestMapping(value="/ywt/xhbsleeppersonlist",method= RequestMethod.GET)
    public ResultBody<List<BhyfStudentSleepInfo>> getXhbSleepPersonList(
            @RequestParam(value = "gid",required = true) Integer gid,
            @RequestParam(value = "gradenum",required = false) Integer gradenum,
            @RequestParam(value = "classid",required = false) Integer classid,
            @RequestParam(value = "startTime",required = false) String startTime,
            @RequestParam(value = "endTime",required = false) String endTime,
            @RequestParam(value = "studentname",required = false) String studentname,
            @RequestParam(value = "personid",required = false) Integer personid,
            @RequestParam(value = "isLast",required = true) Integer isLast
    )
    {
        List<BhyfStudentSleepInfo> returnResult = new ArrayList<>();
        if(studentname==null)studentname="";
        if(gradenum==null)gradenum=0;
        if(classid==null)classid=0;
        List<BhyfStudent> students = studentService.getBhyfStudentList(gid,gradenum,classid,studentname,personid);
        if(isLast==1){
            startTime="2019-09-01";
            endTime= DateTime.now().toString("yyyy-MM-dd");
        }
        List<BhyfXhbSleepPerson> bhyfXhbSleepPersonList = bhyfBdbService.findPersonSleepList(gid, startTime, endTime);
        for(int i=0;i<students.size();i++) {
            BhyfStudent student = students.get(i);
            //学生信息
            BhyfStudentSleepInfo studentSleepInfo = new BhyfStudentSleepInfo();
            studentSleepInfo.setStudent(student);
            //睡眠列表
            int curpersonid = student.getPersonid();
            List<BhyfXhbSleepPerson> studentSleep = new ArrayList<>();
            List<BhyfXhbSleepPerson> sspp = new ArrayList<>();
            studentSleep = bhyfXhbSleepPersonList.stream().filter((BhyfXhbSleepPerson s) -> s.getPersonid().equals(curpersonid)).collect(Collectors.toList());
            if(isLast==1)
            {
                if(studentSleep!=null && studentSleep.size()>0)
                {
                    studentSleep = studentSleep.stream().filter((BhyfXhbSleepPerson s) -> s.getSleepStatus()>0).collect(Collectors.toList());
                    if(studentSleep!=null && studentSleep.size()>0)sspp.add(studentSleep.get(0));
                }
            }else{
                sspp = studentSleep;
            }
            studentSleepInfo.setBhyfXhbSleepPersonList(sspp);
            //关注状态
            /**
            List<BhyfXhbWatch> watches = studentService.findBhyfXhbWatchList(student.getPersonid(), -1,1);
            if (watches != null && watches.size() > 0) {
                studentSleepInfo.setWatchstatus(watches.get(0).getWatchstatus());
                studentSleepInfo.setWatchid(watches.get(0).getId());
            }
             **/
            returnResult.add(studentSleepInfo);
        }
        return ResultBody.ok().data(returnResult);
    }


    @ApiOperation(value = "分配区角信标", notes = "分配区角信标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "angleid", value = "区角id", required = true, paramType = "form"),
            @ApiImplicitParam(name = "beaconaddress", value = "信标地址", required = true, paramType = "form")
    })
    @PostMapping("/ywt/addbhyfbflanglebeacon")
    public ResultBody<Integer> addBhyfBflAngleBeacon(
            @RequestParam(name = "angleid") Integer angleid,
            @RequestParam(name = "beaconaddress") String beaconaddress
    )
    {
        BhyfBflAngleBeacon entiy = new BhyfBflAngleBeacon();
        List<BhyfBflAngleBeacon> list = studentService.isExistBeacon(angleid,beaconaddress);
        if (list != null && list.size() > 0) {
            return ResultBody.failed().data(0);
        }else {
            entiy.setAngleid(angleid);
            entiy.setBeaconaddress(beaconaddress);
            return ResultBody.ok().data(studentService.addBhyfBflAngleBeacon(entiy));
        }
    }


    /**
     * 获取园所课程列表
     * @param gid
     * @return
     */
    @ApiOperation(value = "获取园所课程列表", notes = "获取园所课程列表")
    @RequestMapping(value="/ywt/courselist",method= RequestMethod.GET)
    public ResultBody<List<BhyfBflCourse>> getBhyfCourseList(
            @RequestParam(value = "gid",required = false) final Integer gid
    )
    {
        List<BhyfBflCourse> bhyfBflCourses =  studentService.findBhyfCourseList(gid);
        return ResultBody.ok().data(bhyfBflCourses);
    }

    /**
     * 获取学生的体温晨检记录
     * @param personid
     * @param startdate
     * @param enddate
     * @return
     */
    @ApiOperation(value = "获取学生的体温晨检记录", notes = "获取学生的体温晨检记录")
    @RequestMapping(value="/ywt/person/tempchecklist",method= RequestMethod.GET)
    public ResultBody<BhyfTempCheck> getBhyfTempCheckList(
            @RequestParam(value = "personid",required = true) final Integer personid,
            @RequestParam(value = "startdate",required = true) final String startdate,
            @RequestParam(value = "enddate",required = true) final String enddate
    )
    {
        List<BhyfTempCheck> bhyfTempCheckList =  studentService.getBhyfTempCheckList(personid,startdate,enddate);
        return ResultBody.ok().data(bhyfTempCheckList);
    }


    /**
     * 获取学生的刷卡记录
     * @param personid
     * @param startdate
     * @param enddate
     * @return
     */
    @ApiOperation(value = "获取学生的刷卡记录", notes = "获取学生的刷卡记录")
    @RequestMapping(value="/ywt/person/cardrecordlist",method= RequestMethod.GET)
    public ResultBody<BhyfRecordRemark> getBhyfCardRecordList(
            @RequestParam(value = "personid",required = true) final Integer personid,
            @RequestParam(value = "startdate",required = true) final String startdate,
            @RequestParam(value = "enddate",required = true) final String enddate
    )
    {
        List<BhyfRecordRemark> bhyfRecordRemarkList =  studentService.getBhyfRecordRemarkList(personid,startdate,enddate);
        return ResultBody.ok().data(bhyfRecordRemarkList);
    }

    /**
     * 获取园所刷卡日期分组统计结果
     * @param gid
     * @param startdate
     * @param enddate
     * @return
     */
    @ApiOperation(value = "获取园所刷卡日期分组统计结果", notes = "获取园所刷卡日期分组统计结果")
    @RequestMapping(value="/ywt/garden/dateresult",method= RequestMethod.GET)
    public ResultBody<BhyfCardRecordDateResult> getGardenCardRecordGroupByDate(
            @RequestParam(value = "gid",required = true) final Integer gid,
            @RequestParam(value = "startdate",required = true) final String startdate,
            @RequestParam(value = "enddate",required = true) final String enddate
    )
    {
        List<BhyfCardRecordDateResult> bhyfCardRecordDateResults =  studentService.getGardenCardRecordGroupByDate(gid,startdate,enddate);
        return ResultBody.ok().data(bhyfCardRecordDateResults);
    }


    /**
     * 获取班级刷卡日期分组统计结果
     * @param classid
     * @param startdate
     * @param enddate
     * @return
     */
    @ApiOperation(value = "获取班级刷卡日期分组统计结果", notes = "获取班级刷卡日期分组统计结果")
    @RequestMapping(value="/ywt/class/dateresult",method= RequestMethod.GET)
    public ResultBody<BhyfCardRecordDateResult> getClassCardRecordGroupByDate(
            @RequestParam(value = "classid",required = true) final Integer classid,
            @RequestParam(value = "startdate",required = true) final String startdate,
            @RequestParam(value = "enddate",required = true) final String enddate
    )
    {
        List<BhyfCardRecordDateResult> bhyfCardRecordDateResults =  studentService.getClassCardRecordGroupByDate(classid,startdate,enddate);
        return ResultBody.ok().data(bhyfCardRecordDateResults);
    }


    /**
     * 获取园所刷卡班级分组统计
     * @param gid
     * @param startdate
     * @param enddate
     * @return
     */
    @ApiOperation(value = "获取园所刷卡班级分组统计", notes = "获取园所刷卡班级分组统计")
    @RequestMapping(value="/ywt/garden/classresult",method= RequestMethod.GET)
    public ResultBody<BhyfCardRecordClassResult> getGardenCardRecordGroupByClass(
            @RequestParam(value = "gid",required = true) final Integer gid,
            @RequestParam(value = "startdate",required = true) final String startdate,
            @RequestParam(value = "enddate",required = true) final String enddate
    )
    {
        List<BhyfCardRecordClassResult> bhyfCardRecordDateResults =  studentService.getGardenCardRecordGroupByClass(gid,startdate,enddate);
        return ResultBody.ok().data(bhyfCardRecordDateResults);
    }

    /**
     * 获取园所的班级分组的考勤和体温数据
     * @param gid
     * @param checkdate
     * @return
     */
    @ApiOperation(value = "获取园所的班级分组的考勤和体温数据", notes = "获取园所的班级分组的考勤和体温数据")
    @RequestMapping(value="/ywt/garden/classdata",method= RequestMethod.GET)
    public ResultBody<BhyfGardenData> getGardenClassData(
            @RequestParam(value = "gid",required = true) final Integer gid,
            @RequestParam(value = "checkdate",required = true) final String checkdate
    )
    {
        List<BhyfGardenData> bhyfGardenData =  studentService.getGardenDataGroupByClass(gid,checkdate);
        return ResultBody.ok().data(bhyfGardenData);
    }


    /**
     * 获取班级单日的考勤和体温列表
     * @param gid
     * @param checkdate
     * @return
     */
    @ApiOperation(value = "获取班级单日的考勤和体温列表", notes = "获取班级单日的考勤和体温列表")
    @RequestMapping(value="/ywt/garden/classdatalist",method= RequestMethod.GET)
    public ResultBody<BhyfClassData> getClassDataListByDate(
            @RequestParam(value = "gid",required = true) final Integer gid,
            @RequestParam(value = "classid",required = false) Integer classid,
            @RequestParam(value = "checkdate",required = true) final String checkdate
    )
    {
        if(classid==null)classid=0;
        List<BhyfClassData> bhyfClassData =  studentService.getClassDataListByDate(gid,classid,checkdate);
        return ResultBody.ok().data(bhyfClassData);
    }


    /**
     * 获取园所列表（关键字查询）
     * @param keyword
     * @return
     */
    @ApiOperation(value = "获取园所列表（关键字查询）", notes = "获取园所列表（关键字查询）")
    @RequestMapping(value="/ywt/gardenlist",method= RequestMethod.GET)
    public ResultBody<BhyfGardenInfo> getGardenInfoListByKeyword(
            @RequestParam(value = "keyword",required = false) final String keyword,
            @RequestParam(value = "provinceid",required = false) final Integer provinceid,
            @RequestParam(value = "gid",required = false) final Integer gid
    )
    {
        List<BhyfGardenInfo> bhyfGardenInfoList =  studentService.getGardenInfoList(keyword,provinceid,gid);
        return ResultBody.ok().data(bhyfGardenInfoList);
    }

    /**
     * 删除区角信标
     * @param angleid
     * @param beaconaddress
     * @return
     */
    @ApiOperation(value = "删除区角信标", notes = "删除区角信标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "angleid", value = "区角id", required = true, paramType = "form"),
            @ApiImplicitParam(name = "beaconaddress", value = "信标地址", required = true, paramType = "form")
    })
    @PostMapping("/ywt/deletebhyfbflanglebeacon")
    public ResultBody<Integer> deleteBhyfBflAngleBeacon(
            @RequestParam(name = "angleid") Integer angleid,
            @RequestParam(name = "beaconaddress") String beaconaddress
    )
    {
        BhyfBflAngleBeacon entiy = new BhyfBflAngleBeacon();
        entiy.setAngleid(angleid);
        entiy.setBeaconaddress(beaconaddress);
        return ResultBody.ok().data(studentService.deleteBhyfBflAngleBeacon(entiy));
    }

    /**
     * 停用区角
     * @param id
     * @return
     */
    @ApiOperation(value = "停用区角", notes = "停用区角")
    @PostMapping("/ywt/stopangle")
    public ResultBody<Integer> updateBhyfBflAngleStatus(
            @RequestParam(name = "id") Integer id
    )
    {
        BhyfBflAngle entiy = studentService.getBhyfBflAngleById(id);
        if(entiy!=null) {
            entiy.setStatus(0);
            return ResultBody.ok().data(studentService.updateBhyfBflAngle(entiy));
        }else{
            return ResultBody.failed().data(-1);
        }
    }

    /**
     * 重置区角
     * @param id
     * @param anglename
     * @param angletype
     * @param makeobject
     * @param makemethod
     * @return
     */
    @ApiOperation(value = "重置区角", notes = "重置区角")
    @PostMapping("/ywt/resetangle")
    public ResultBody<Integer> resetBhyfBflAngle(
            @RequestParam(name = "id") Integer id,
            @RequestParam(name = "anglename") String anglename,
            @RequestParam(name = "angletype") Integer angletype,
            @RequestParam(name = "makeobject") String makeobject,
            @RequestParam(name = "makemethod") String makemethod
    )
    {
        BhyfBflAngle entiy = studentService.getBhyfBflAngleById(id);
        if(entiy!=null) {
            entiy.setStatus(0);
            List<BhyfBflAngleBeacon> beaconlist = new ArrayList<>();
            beaconlist = studentService.getBhyfClassAngleBeaconList(id);
            studentService.updateBhyfBflAngle(entiy);
            BhyfBflAngle newentiy = new BhyfBflAngle();
            newentiy.setGid(entiy.getGid());
            newentiy.setGradenum(entiy.getGradenum());
            newentiy.setAngletype(angletype);
            newentiy.setAnglename(anglename);
            newentiy.setClassid(entiy.getClassid());
            newentiy.setClassname(entiy.getClassname());
            newentiy.setStatus(1);
            newentiy.setMakeobject(makeobject);
            newentiy.setMakemethod(makemethod);
            newentiy = studentService.addBhyfBflAngle(newentiy);
            if(beaconlist!=null) {
                for (BhyfBflAngleBeacon beacon : beaconlist) {
                    studentService.deleteBhyfBflAngleBeacon(beacon);
                    beacon.setAngleid(newentiy.getId());
                    beacon.setBeaconaddress(beacon.getBeaconaddress());
                    studentService.addBhyfBflAngleBeacon(beacon);
                }
            }
            return ResultBody.ok().data(newentiy);
        }else{
            return ResultBody.failed().data(-1);
        }
    }

    @ApiOperation(value = "获取园所所有学生列表", notes = "获取园所所有学生列表")
    @RequestMapping(value="/ywt/allstudentlist",method= RequestMethod.GET)
    public ResultBody<List<BhyfStudent>> getAllBhyfStudentList(
            @RequestParam(value = "gid",required = true) final Integer gid,
            @RequestParam(value = "classid",required = true) final Integer classid
    )
    {
        List<BhyfStudent> students =  studentService.getAllBhyfStudentList(gid,classid,"",0);
        return ResultBody.ok().data(students);
    }

}
